import { Controller } from 'egg';
import { EnumType } from '../model/enum'

export default class EnumController extends Controller {

  validateBase = {
    name: this.ctx.w_validate_otherName,
    index: {
      type: 'number',
      min: 0,
      max: 9,
      convertType: 'number',
    },
    description: this.ctx.w_validate_description,
  }

  public async create() {
    this.ctx.validate({
      superId: {
        type: 'mongo-id',
      },
      isProject: {
        type: 'bool',
        default: true
      },
      project: {
        type: 'mongo-id',
      },
      type: {
        type: 'enum',
        values: [ EnumType.FOLDER, EnumType.FILE ],
      },
      ...this.validateBase
    });
    const { type } = this.ctx.w_body()
    let exitKey = 'name type index description superId isProject project'
    if (type === EnumType.FILE) {
      this.ctx.validate({
        path: this.ctx.w_validate_pagePath,
        uuid: this.ctx.w_validate_uuid,
      });
      exitKey = `${exitKey} path uuid`
    }
    await this.ctx.service.enum.create(this.ctx.w_body(exitKey));
    this.ctx.sendSuccess('OK');
  }

  public async update() {
    this.ctx.w_validate_param_id()
    this.ctx.validate(this.validateBase);
    const { uuid, path } = this.ctx.w_body()
    await this.ctx.service.enum.update(this.ctx.params.id, this.ctx.w_body('name index description'), uuid, path);
    this.ctx.sendSuccess('OK');
  }

  /**
   * 删除数据
   */
  public async delete() {
    this.ctx.w_validate_param_id()
    this.ctx.validate({
      superId: {
        type: 'mongo-id',
      },
    }, this.ctx.query);
    await this.ctx.service.enum.delete(this.ctx.params.id, this.ctx.query.superId);
    this.ctx.sendSuccess('OK');
  }

  /**
   * 获取详情
   */
  public async show() {
    this.ctx.w_validate_param_id()
    const model = await this.ctx.service.enum.show(this.ctx.params.id);
    this.ctx.sendSuccess(model);
  }

  /**
   * 更新数据说明
   */
  public async updateDataShow() {
    this.ctx.w_validate_param_id()
    this.ctx.validate({
      dataShow: {
        type: 'string',
      },
    });
    const model = await this.ctx.service.enum.updateDataShow(this.ctx.params.id, this.ctx.w_body().dataShow);
    this.ctx.sendSuccess(model);
  }
}
